import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';

import { NavController } from 'ionic-angular';
import { IonicPage } from 'ionic-angular';
import { UserData } from '../../providers/user-data';
import { OauthService } from '../../providers/oauth-service';



@IonicPage()
@Component({
  selector: 'page-user',
  templateUrl: 'login.html'
})
export class LoginPage {
  login: {username?: string, password?: string} = {};
  submitted = false;
  logincheck = true;
  constructor(public navCtrl: NavController, public userData: UserData,public oauthService:OauthService) { }

  onLogin(form: NgForm) {
    this.submitted = true;
    this.logincheck = true;
    if (form.valid) {
      let res = this.oauthService.userLogin(this.login);
      res.subscribe(
                data => {
                  this.userData.saveToken(JSON.stringify(data));
                  console.log(this.userData.checkTokenExpires());
                  console.log('success:' +typeof(data)+"----"+ JSON.stringify(data));
                  this.navCtrl.push('TabsPage');
                },
                error => {
                  console.log('error:'+JSON.stringify(error));
                },
                ()=>{
                  console.log("完了");
                }
            );
      this.logincheck = false;
    }
  }
}
